package com.rongc.client.freight.business.supply.view.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.network.http.RequestManager;
import com.rongc.client.core.utils.LogUtils;
import com.rongc.client.freight.R;
import com.rongc.client.freight.UniApplication;
import com.rongc.client.freight.base.BaseActivity;
import com.rongc.client.freight.base.model.BaseDic;
import com.rongc.client.freight.base.view.adapter.RecyclerBaseAdapter;
import com.rongc.client.freight.base.view.widget.DividerItemDecoration;
import com.rongc.client.freight.business.carrier.model.ConveyanceDic;
import com.rongc.client.freight.business.supply.model.SupplyBean;
import com.rongc.client.freight.business.supply.model.SupplyDic;
import com.rongc.client.freight.business.supply.model.SupplyMyList;
import com.rongc.client.freight.business.supply.request.api.SupplyMyListApi;
import com.rongc.client.freight.business.supply.view.adapter.RecyclerSupplyListAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by ccqx on 2017/7/1.
 */

public class SupplyWaybillListActivity extends BaseActivity {
    @Bind(R.id.recycler)
    RecyclerView mRecyclerView;
    RecyclerSupplyListAdapter mLvAdapter;

    List<SupplyBean> lsNews;

    Response.Listener<SupplyMyList> respNewsListener = new Response.Listener<SupplyMyList>() {

        @Override
        public void onResponse(SupplyMyList response) {
            if (HandleCode.requestSuccess()) {
                if(response != null && response.getList() != null){
                    lsNews.clear();
                    lsNews.addAll(response.getList());
                    mLvAdapter.notifyDataSetChanged();
                }


//                mLvAdapter.notifyDataSetChanged();
            }
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            LogUtils.i("error.getMessage(): " + error.getMessage());
        }
    };

    @Override
    public void initView() {
        initToolbar(R.string.supply_waybilllist_title);

        lsNews = new ArrayList<>();

        mLvAdapter = new RecyclerSupplyListAdapter(this, lsNews);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        mRecyclerView.setAdapter(mLvAdapter);

        mLvAdapter.setOnItemClickListener(new RecyclerBaseAdapter.OnItemClickListener<SupplyBean>() {
            @Override
            public void onItemClick(View view, int position, SupplyBean data) {
                switch (UniApplication.getInstance().getType()) {
                    case BaseDic.IdentityType.DRIVING_TYPE:
                        ConveyanceDic.jumpActivity(SupplyWaybillListActivity.this,position,data);
                        break;
                    case BaseDic.IdentityType.OWNER_TYPE:
                        SupplyDic.jumpActivity(SupplyWaybillListActivity.this,position,data);
                        break;
                }
            }
        });


        RequestManager.getInstance().call(new SupplyMyListApi(new SupplyMyListApi.SupplyMyListParams(
                UniApplication.getInstance().getUserInfo().getUserId(),"1"),
                respNewsListener, errorListener));
    }

    @Override
    public int getLayoutId() {
        return R.layout.universal_recycler;
    }
}
